/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.modeldocument.primitives;

import java.util.LinkedHashSet;

import dgbgui.modeldocument.ModelDocument;

public class PrimitiveLabelFactory {
    
    static private String getSpecialCaseLabel(ConceptronType instance) {
        // No special cases for types
        return null;
    }
    
    static public String getLabel(ConceptronType type) {
        
        String label = getSpecialCaseLabel(type);
        if ( label != null ) {
            return label;
        }
        
        return type.m_name;
    }
    
    static private String getSpecialCaseLabel(Conceptron instance) {
        //if ( )
        ConceptronType type = ModelDocument.getInstance().getConceptronType(instance);
        if ( type.m_name.equalsIgnoreCase("gene_ontology") ) {
            for ( Attribute attr : instance.m_attributes ) {
                AttributeType attributeType = ModelDocument.getInstance().getAttributeType(attr);
                if ( attributeType.m_name.equalsIgnoreCase("namespace")) {
                    return attr.m_value.toString();
                }
            }
        }
        return null;
    }
    
    static public String getIdentifierLabel(Conceptron instance) {
        for ( Attribute attr : instance.m_attributes ) {
            AttributeType attributeType = ModelDocument.getInstance().getAttributeType(attr);
            if (attributeType.m_isIdentifier ) {
                return (String)attr.m_value.toString();
            }
        }
        return null;
    }
    
    static public String getLabel(Conceptron instance) {
        
        String label = getSpecialCaseLabel(instance);
        if ( label != null ) {
            return label;
        }
        
        // First try the identifier attribute
        label =  getIdentifierLabel(instance);

        // Then try the following string, in preference order
        if ( label == null ) {
            LinkedHashSet<String> preferredLabels = new LinkedHashSet<String>();
            preferredLabels.add("name");
            preferredLabels.add("namespace");
            preferredLabels.add("id");
            for ( String s : preferredLabels ) {
                if ( label != null ) {
                    break;
                }
                for ( Attribute attr : instance.m_attributes ) {
                    AttributeType attributeType = ModelDocument.getInstance().getAttributeType(attr);
                    if (attributeType.m_name.equalsIgnoreCase(s)) {
                        label = (String)attr.m_value.toString();    
                        break;
                    }
                }
                
            }
        }
        
        // If the label is still null (either not found, or no attributes),
        // use the type name plus id
        if ( label == null ) {
            ConceptronType type = ModelDocument.getInstance().getConceptronType(instance);
            if ( type != null ) {
                label = type.m_name; 
                    //+ ":" + instance.m_id;
            } else {
                label = "???";
            }
        }
        return label;
    }

    
    
    

}
